from selenium.webdriver.support.select import Select

from .multiple import run_multiple_operation
from utils.selenium.perform import WPerform


class SelectType:
    """下拉框"""
    index = 1
    value = 2
    text = 3


select_func_dic = {
    SelectType.index: 'select_by_index',
    SelectType.value: 'select_by_value',
    SelectType.text: 'select_by_visible_text'
}


def select_func(perform: WPerform, element, item: dict, *args):
    select_type = item.get('select_type')
    select_query = item.get('select_query')
    select_obj = Select(element)
    sel_func_name = select_func_dic.get(select_type)
    getattr(select_obj, sel_func_name)(select_query)


def run_select_operation(perform: WPerform, item, other_query: dict):
    """
    执行选择器
    :param driver:
    :param item: 操作数据
    :param other_query: 额外参数
    :return:
    """
    run_multiple_operation(perform, item, select_func, (), other_query)
